package com.bsoft.gol.hcs.task;

import ctd.job.annotation.ScheduleJob;
import ctd.job.pojo.JobParameter;
import ctd.util.context.ContextUtils;
import hcn.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bsoft.gol.hcs.specialist.dao.*;
import com.bsoft.gol.hcs.specialist.entity.InspectReportRecordEntity;
import com.bsoft.gol.hcs.specialist.entity.InspectReportRoutineDetailEntity;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.visitrecord.dao.*;
import com.bsoft.gol.hcs.visitrecord.entity.*;
import org.atag.util.common.SpringPropertiesUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: lanyu
 * @Description:his同步数据过期处理
 * @Date: create in 2022/6/8 9:54
 */
@Component
public class HospitalVisitsRecordOverdueTask {

    @Autowired
    VrOutpatientVisitRecordDAO vrOutpatientVisitRecordDAO;
    @Autowired
    VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;
    @Autowired
    InspectReportRecordDAO inspectReportRecordDAO;
    @Autowired
    InspectReportRoutineDetailDAO inspectReportRoutineDetailDAO;
    @Autowired
    ExamReportDAO examReportDAO;
    @Autowired
    ExamReportImageDAO examReportImageDAO;
    @Autowired
    private VrVisitDiagnosisDAO vrVisitDiagnosisDAO;
    @Autowired
    private TreatmentRecordDAO treatmentRecordDAO;
    @Autowired
    private PrescriptionRecordDAO prescriptionRecordDAO;
    @Autowired
    private VrApplicationFormDetailDAO vrApplicationFormDetailDAO;
    @Autowired
    private VrApplicationFormRecordDAO vrApplicationFormRecordDAO;
    @Autowired
    private VrSurgicalRecordDAO vrSurgicalRecordDAO;
    @Autowired
    VrSynchroSettingsDAO vrSynchroSettingsDAO;

    /**
     * his过期数据删除任务执行
     */
    @ScheduleJob(jobName = "syncDeleteHospitalVisitRecord", cron = "0 0 20 * * ?", shardingTotalCount = 1, description =
            "his过期数据删除任务", overwrite = true)
    public void syncDeleteHospitalVisitRecord(JobParameter jobParameter) throws Exception {

        String tenantId = (String) ContextUtils.get("$tenantId");
        if(null == tenantId){
            ContextUtils.put("$tenantId", SpringPropertiesUtil.get("tenantId"));
        }

        //1.获取同步任务参数设置信息列表
        List<VrSynchroSettings> synchroSettingsList = vrSynchroSettingsDAO.queryVrSynchroSettingsListByMonth();
        if (CommonUtils.isNotEmpty(synchroSettingsList)) {
            for (VrSynchroSettings vrSynchroSettings : synchroSettingsList) {
                switchDeleteHospitalVisitRecord(vrSynchroSettings.getSettingType(), vrSynchroSettings.getStorageMonth());
            }
        }
    }

    /**
     * 根据类型删除his同步数据
     *
     * @param settingType
     * @param storageMonth
     */
    private void switchDeleteHospitalVisitRecord(Integer settingType, Integer storageMonth) {
        //得到一个Calendar的实例
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, -storageMonth);
        Date resultDate = ca.getTime();
        switch (settingType) {
            case 1:
                vrOutpatientVisitRecordDAO.deleteOutpatientOverdueData(resultDate);
                break;
            case 2:
                deleteInpatientRecord(resultDate);
                break;
            case 3:
                break;
            case 4:
                deleteInspectReportRecord(resultDate);
                break;
            case 5:
                examReportDAO.deleteInspectReportOverdueData(resultDate);
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                deleteOrderReportRecord(resultDate);
                break;
            case 9:
                vrSurgicalRecordDAO.deleteSurgicalOverdueData(resultDate);
                break;
            default:
                break;
        }
    }

    /**
     * 删除住院记录信息
     * @param resultDate
     */
    private void deleteInpatientRecord(Date resultDate) {
        //1.查询住院记录信息
        List<VrInpatientVisitRecord> visitRecords = vrInpatientVisitRecordDAO.queryInpatientOverdueList(resultDate);
        if (CommonUtils.isNotEmpty(visitRecords)) {
            for (VrInpatientVisitRecord visitRecord : visitRecords) {
                //2.1.查询诊断信息
                List<VrVisitDiagnosis> vrVisitDiagnoses = vrVisitDiagnosisDAO.queryVrVisitDiagnosis(visitRecord.getHospitalRecordId());
                if (CommonUtils.isNotEmpty(vrVisitDiagnoses)) {
                    vrVisitDiagnoses.forEach(vrVisitDiagnose -> {
                        //2.2.删除诊断信息
                        vrVisitDiagnosisDAO.remove(vrVisitDiagnose.getDiagnosticRecordId());
                    });
                }
                //3.删除住院记录
                vrInpatientVisitRecordDAO.remove(visitRecord.getHospitalRecordId());
            }
        }


    }

    /**
     * 删除检验报告以及详细信息
     *
     * @param resultDate
     */
    private void deleteInspectReportRecord(Date resultDate) {
        //1.先查询检验主表信息
        List<InspectReportRecordEntity> reportRecordList = inspectReportRecordDAO.queryInspectReportRecordOverdueList(resultDate);
        if (CommonUtils.isNotEmpty(reportRecordList)) {
            for (InspectReportRecordEntity reportRecordEntity : reportRecordList) {
                //2.1根据检验报告id获取报告详情信息
                List<InspectReportRoutineDetailEntity> detailList =
                        inspectReportRoutineDetailDAO.queryReportRoutineDetailList(reportRecordEntity.getInspectReportRecordId());
                if (CommonUtils.isNotEmpty(detailList)) {
                    //2.2.删除子表信息
                    for (InspectReportRoutineDetailEntity detailEntity : detailList) {
                        inspectReportRoutineDetailDAO.remove(detailEntity.getInspectReportRoutineDetailId());
                    }
                }
                //3.删除主表信息
                inspectReportRecordDAO.remove(reportRecordEntity.getInspectReportRecordId());
            }
        }
    }

    /**
     * 删除医嘱信息
     * @param resultDate
     */
    private void deleteOrderReportRecord(Date resultDate){
        //1.删除用药记录
        prescriptionRecordDAO.deletePrescriptOverdueData(resultDate);
        //2.删除治疗信息
        treatmentRecordDAO.deleteTreatmentOverdueData(resultDate);
        //3.删除检验检查申请单
        List<VrApplicationFormRecord> formRecordList = vrApplicationFormRecordDAO.queryApplicationFormRecordList(resultDate);
        if (CommonUtils.isNotEmpty(formRecordList)){
            for (VrApplicationFormRecord formRecord : formRecordList) {
                //3.1.获取检验检查申请单详情
                List<VrApplicationFormDetail> formDetails = vrApplicationFormDetailDAO.queryDetailByApplicationId(formRecord.getApplicationId());
                if (CommonUtils.isNotEmpty(formDetails)){
                    //3.2.删除详情
                    for (VrApplicationFormDetail formDetail : formDetails) {
                        vrApplicationFormDetailDAO.remove(formDetail.getApplicationDetailId());
                    }
                }
                //3.3.删除检验检查申请单
                vrApplicationFormRecordDAO.remove(formRecord.getApplicationId());
            }
        }


    }
}
